package org.apache.derby.impl.drda;

import org.apache.derby.iapi.jdbc.EngineConnection;

import java.sql.SQLException;
import java.util.Enumeration;
import java.util.Hashtable;

/**
 * @author ex_chenzy59
 * @since 2025/3/6
 *
 */
public class Database {
    private Hashtable<Object, DRDAStatement> stmtTable;
    DRDAStatement defaultStatement;
    private EngineConnection conn;
    private DRDAStatement currentStatement;
    protected void close() throws SQLException
    {

        try {
            if (stmtTable != null)
            {
                for (Enumeration e = stmtTable.elements(); e.hasMoreElements() ;)
                {
                    ((DRDAStatement) e.nextElement()).close();
                }

            }
            if (defaultStatement != null)
                defaultStatement.close();
            if ((conn != null) && !conn.isClosed())
            {
                //rollback all the pending transactions except global XA trans
                if (! conn.isInGlobalTransaction())
                {
                    conn.rollback();
                }
                conn.close();
            }
        }
        finally {
            conn = null;
            currentStatement = null;
            defaultStatement = null;
            stmtTable=null;
        }
    }
}
